<?php
/**
 * Check access authority
 *
 * @author     Mario Schillermann <info@rockit-flash.com>
 * @encoding   UTF-8 äöüßÖÄÜ
 * @copyright  © 2011 Mario Schillermann (info@rockit-flash.com)
 * @license    GNU GPL v2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
 * @uses
 * @category   Library
 * @package    App
 * @subpackage Helper
 * @todo
 */

class App_Controller_Helper_Acl extends Zend_Controller_Action_Helper_Abstract
{
    /**
     * Check access authority to resource
     *
     * @param string $module
     * @param string $controller
     * @param string $action
     * 
     * @return boolean
     */
    static function isAllowed($action = null, $controller = null, $module = null)
    {
        try {
            $auth = Zend_Auth::getInstance();
            $user = $auth->getStorage()->read();
            $role = current($user['acl']->getRoles());
            
            if ($role == "admin") {
                return true;
            }
            
            if ($module === null) {
                $module = $this->getRequest()->getModuleName();
            }
            if ($controller === null) {
                $controller = $this->getRequest()->getControllerName();
            }

            if ($action === null) {
                $action = $this->getRequest()->getActionName();
            }

            $resource = $module . "_" . $controller;

            if ($user['acl']->has($resource)) {
                return $user['acl']->isAllowed(
                    $user['username'],
                    $resource,
                    $action
                );
            }
            return false;
        } catch (Zend_Exception $e) {
            error_log(print_r($e, true));
            return false;
        }
    }
}